#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#include <config.h>
#include <page_manager.h>
#include <photo_manager.h>
#include <weather.h>

static int mainpageRun(void);
static int mainpageGetInputEvent(void);
unsigned int timeCnt;
static char strdateBak[64];
static char strtimeBak[32];

static T_pageAction mainpage = 
{
	.name 			= "mainpage",
	.run  			= mainpageRun,
	.getInputEvent 	= mainpageGetInputEvent,
	.next 			= NULL,
};

static T_iconInfo mainpageIcon[] =
{
	{700,150,"NULL"},
	{700,450,"NULL"},
	{910,450,"NULL"},
	{1120,450,"NULL"},
	{0,0,"NULL"},
};

static T_touchInfo setCity[]=
{
	{1170,0,20,0,"icon/setting.png"},
};

static void showMainPage(void)
{
	T_imageInfo imageData;
	
	g_ptDisplayDevice->displayDeviceClear(0xffffff);
	g_ptDisplayDevice->displayDrawLine(650,360,600,10,0xff);
	g_ptDisplayDevice->displayDrawLine(860,400,10,250,0xff);
	g_ptDisplayDevice->displayDrawLine(1070,400,10,250,0xff);

	if(strstr(setCity[0].icon,".bmp")==0 || strstr(setCity[0].icon,".jpg")==0 || strstr(setCity[0].icon,".png")==0)
	{
		getPhotoData(setCity[0].icon, &imageData);
		setCity[0].xMax = setCity[0].xMin + imageData.width;
		setCity[0].yMax = setCity[0].yMin + imageData.height;
		showImage(setCity[0].xMin, setCity[0].yMin, &imageData);
	}
}

static int getWeekDay(int weekday, int lenth, char *buff)
{
	if(weekday<0 || weekday>=7 || buff==NULL)
	{
		fprintf(stderr,"arguement error\n");
		return -1;
	}

	switch(weekday)
	{
		case 0:
			snprintf(buff,lenth,"星期天");
			break;
		case 1:
			snprintf(buff,lenth,"星期一");
			break;
		case 2:
			snprintf(buff,lenth,"星期二");
			break;
		case 3:
			snprintf(buff,lenth,"星期三");
			break;
		case 4:
			snprintf(buff,lenth,"星期四");
			break;
		case 5:
			snprintf(buff,lenth,"星期五");
			break;
		case 6:
			snprintf(buff,lenth,"星期六");
			break;
		default:
			break;
	}

	return 0;
}

static int showTime(void)
{
	char strdate[64]={0};
	char strtime[32]={0};
	char strweek[32]={0};
	struct tm result;
	struct tm *me;
	int temp;

	time_t currentTime = time(NULL);
	me = localtime_r(&currentTime, &result);
	if(me==NULL)
	{
		fprintf(stderr,"localtime_r error:%s\n",strerror(errno));
		return -1;
	}
	snprintf(strdate,63,"%d年%02d月%02d日",result.tm_year+1900, result.tm_mon+1, result.tm_mday);
	snprintf(strtime,31,"%02d:%02d",result.tm_hour, result.tm_min);

	if(strncmp(strdate,strdateBak,64)!=0)
	{
		//年月日星期显示
		g_ptDisplayDevice->displayDeviceClearArea(0,0,LCDWIDTH/2,LCDHEIGHT/2,0xffffff);
		freetypeShowStr(50, 90, 70, 11, strdate, 0xff, 0xffffff);
		strncpy(strdateBak,strdate,64);
		
		getWeekDay(result.tm_wday, 31, strweek);
		freetypeShowStr(200, 220, 70, 3, strweek, 0xff, 0xffffff);

		g_ptDisplayDevice->displayDeviceClearArea(mainpageIcon[1].xStart-15,370,mainpageIcon[1].xStart+40,425,0xffffff);
		freetypeShowStr(mainpageIcon[1].xStart-15, 370, 50, 3, strweek, 0xff, 0xffffff);
		
		temp = result.tm_wday+1;
		if(temp>=7) temp=temp%7;
		getWeekDay(temp, 31, strweek);
		g_ptDisplayDevice->displayDeviceClearArea(mainpageIcon[2].xStart-15,370,mainpageIcon[2].xStart+40,425,0xffffff);
		freetypeShowStr(mainpageIcon[2].xStart-15, 370, 50, 3, strweek, 0xff, 0xffffff);
		
		temp = result.tm_wday+2;
		if(temp>=7) temp=temp%7;
		getWeekDay(temp, 31, strweek);
		g_ptDisplayDevice->displayDeviceClearArea(mainpageIcon[3].xStart-15,370,mainpageIcon[3].xStart+40,425,0xffffff);
		freetypeShowStr(mainpageIcon[3].xStart-15, 370, 50, 3, strweek, 0xff, 0xffffff);
	}
	if(strncmp(strtime,strtimeBak,32)!=0)
	{
		g_ptDisplayDevice->displayDeviceClearArea(0,LCDHEIGHT/2,LCDWIDTH/2,LCDHEIGHT,0xffffff);
		freetypeShowStr(50, 380, 200, 10, strtime, 0xff, 0xffffff);
		
		strncpy(strtimeBak,strtime,32);
	}
	
	return 0;
}

static void getWeatherIcon(char *text_day, char *iconDir)
{
	if(strstr(text_day,"晴") !=NULL)
		memcpy(iconDir, "icon/0.png", 11);
	else if(strstr(text_day,"多云") !=NULL)
		memcpy(iconDir, "icon/3.png", 11);
	else if(strstr(text_day,"阴") !=NULL)
		memcpy(iconDir, "icon/2.png", 11);
	else if(strstr(text_day,"雨") !=NULL)
		memcpy(iconDir, "icon/9.png", 11);
	else if(strstr(text_day,"雪") !=NULL)
		memcpy(iconDir, "icon/14.png", 12);
	else if(strstr(text_day,"尘") !=NULL)
		memcpy(iconDir, "icon/19.png", 12);
	else if(strstr(text_day,"沙") !=NULL)
		memcpy(iconDir, "icon/17.png", 12);
	else if(strstr(text_day,"雾") !=NULL)
		memcpy(iconDir, "icon/18.png", 12);
	else if(strstr(text_day,"霾") !=NULL)
		memcpy(iconDir, "icon/16.png", 12);
	else if(strstr(text_day,"风") !=NULL)
		memcpy(iconDir, "icon/20.png", 12);
	else if(strstr(text_day,"冷") !=NULL)
		memcpy(iconDir, "icon/23.png", 12);
	else
		memcpy(iconDir, "icon/24.png", 12);
}

static void showDateWeather(void)
{
	T_imageInfo imageData;
	int i;
	int num;
	PT_futureWeather futureWeather;
	char day1Info[128] = {0};
	char day2Info[128] = {0};
	char day3Info[128] = {0};
	char day1text_day[WEATHERSIZE] = {0};
	char day2text_day[WEATHERSIZE] = {0};
	char day3text_day[WEATHERSIZE] = {0};

	pthread_mutex_lock(&weatherMutex);
	futureWeather = (PT_futureWeather)g_pFutureWeatherBuff;
	snprintf(day1Info,127,"%s~%s℃",futureWeather->low,futureWeather->high);
	memcpy(day1text_day,futureWeather->text_day,WEATHERSIZE);
	getWeatherIcon(futureWeather->text_day, mainpageIcon[1].icon);
	
	futureWeather +=1;
	snprintf(day2Info,127,"%s~%s℃",futureWeather->low,futureWeather->high);
	memcpy(day2text_day,futureWeather->text_day,WEATHERSIZE);
	getWeatherIcon(futureWeather->text_day, mainpageIcon[2].icon);
	
	futureWeather +=1;
	snprintf(day3Info,127,"%s~%s℃",futureWeather->low,futureWeather->high);
	memcpy(day3text_day,futureWeather->text_day,WEATHERSIZE);
	getWeatherIcon(futureWeather->text_day, mainpageIcon[3].icon);
	pthread_mutex_unlock(&weatherMutex);

	//显示天气
	g_ptDisplayDevice->displayDeviceClearArea(mainpageIcon[1].xStart-15,425,860,LCDHEIGHT,0xffffff);
	freetypeShowStr(mainpageIcon[1].xStart+20, 570, 35, 6, day1text_day, 0xff, 0xffffff);
	freetypeShowStr(mainpageIcon[1].xStart-10, 610, 35, 8, day1Info, 0xff, 0xffffff);

	g_ptDisplayDevice->displayDeviceClearArea(mainpageIcon[2].xStart-15,425,1070,LCDHEIGHT,0xffffff);
	freetypeShowStr(mainpageIcon[2].xStart+20, 570, 35, 6, day2text_day, 0xff, 0xffffff);
	freetypeShowStr(mainpageIcon[2].xStart-10, 610, 35, 8, day2Info, 0xff, 0xffffff);

	g_ptDisplayDevice->displayDeviceClearArea(mainpageIcon[3].xStart-15,425,LCDWIDTH,LCDHEIGHT,0xffffff);
	freetypeShowStr(mainpageIcon[3].xStart+20, 570, 35, 6, day3text_day, 0xff, 0xffffff);
	freetypeShowStr(mainpageIcon[3].xStart-10, 610, 35, 8, day3Info, 0xff, 0xffffff);

	//显示图标
	num = sizeof(mainpageIcon)/sizeof(mainpageIcon[0]);
	for(i=1; i<num; i++)
	{
		if(strcmp(mainpageIcon[i].icon,"NULL")==0)
			break;
		else if(strstr(mainpageIcon[i].icon,".bmp")==0 || strstr(mainpageIcon[i].icon,".jpg")==0 || strstr(mainpageIcon[i].icon,".png")==0)
		{
			getPhotoData(mainpageIcon[i].icon, &imageData);
			showImage(mainpageIcon[i].xStart, mainpageIcon[i].yStart, &imageData);
		}
	}
}

static void showCurrentWeather(void)
{
	PT_currentWeather currentWeather;
	PT_lifeIndex   	  lifeIndex; 
	T_imageInfo imageData;
	char currentWeatherIcon[ICONNAMESIZE]={0};
	char currentCityName[WEATHERSIZE]={0};
	char currentTemperature[ICONNAMESIZE]={0};
	char currentInfo1[ICONNAMESIZE]={0};
	char currentInfo2[ICONNAMESIZE]={0};

	pthread_mutex_lock(&weatherMutex);
	currentWeather = (PT_currentWeather)g_pCurrentWeatherBuff;
	getWeatherIcon(currentWeather->text, currentWeatherIcon);
	memcpy(currentCityName,currentWeather->cityName,strlen(currentWeather->cityName));
	snprintf(currentTemperature,ICONNAMESIZE-1,"室外温度:%s℃",currentWeather->temperature);
	
	lifeIndex = (PT_lifeIndex)g_pLifeIndexBuff;	
	snprintf(currentInfo1,ICONNAMESIZE-1,"空气:%s  体感:%s",lifeIndex->air_pollution,lifeIndex->dressing);
	snprintf(currentInfo2,ICONNAMESIZE-1,"紫外线强度:%s",lifeIndex->uv);
	pthread_mutex_unlock(&weatherMutex);

	g_ptDisplayDevice->displayDeviceClearArea(650,0,LCDWIDTH-110,110,0xffffff);
	g_ptDisplayDevice->displayDeviceClearArea(650,110,LCDWIDTH,350,0xffffff);
	
	if(strstr(currentWeatherIcon,".bmp")==0 || strstr(currentWeatherIcon,".jpg")==0 || strstr(currentWeatherIcon,".png")==0)
	{
		getPhotoData(currentWeatherIcon, &imageData);
		showImage(650, 200, &imageData);
	}

	freetypeShowStr(700, 10, 50, 12, currentInfo1, 0xff, 0xffffff);
	freetypeShowStr(700, 90, 50, 12, currentInfo2, 0xff, 0xffffff);
	freetypeShowStr(850, 180, 50, 4, currentCityName, 0xEB8E55, 0xffffff);
	freetypeShowStr(850, 260, 50, 9, currentTemperature, 0xEB8E55, 0xffffff);
}

static void handle_alarm(int num)
{
	int ret;

	ret = showTime();
	if(ret<0)
	{
		fprintf(stderr,"showTime error\n");
	}

	if(timeCnt%600 ==0 && g_weatherFlag ==1)
	{
		showDateWeather();
		showCurrentWeather();
	}
	timeCnt++;
}

static int mainpageRun(void)
{
	int ret;
	struct itimerval new_value;
	struct itimerval old_value;

	//showpage
	showMainPage();
	handle_alarm(1);

	//start timer
	signal(SIGALRM, handle_alarm);
	new_value.it_interval.tv_sec 	= 1;
	new_value.it_interval.tv_usec 	= 0;
	new_value.it_value.tv_sec 		= 1;
	new_value.it_value.tv_usec 		= 0;
	ret = setitimer(ITIMER_REAL, &new_value, &old_value);
	if(ret <0)
	{
		fprintf(stderr,"setitimer error %s\n",strerror(errno));
		return -1;
	}

	while(1)
	{
		ret = mainpageGetInputEvent();
		if(ret ==1)
		{
			ret = setitimer(ITIMER_REAL, &old_value, NULL);
			if(ret <0)
			{
				fprintf(stderr,"setitimer error %s\n",strerror(errno));
				return -1;
			}

			timeCnt =0;
			memset(strdateBak,0,64);
			memset(strtimeBak,0,32);
			runPage("settingpage");
		}
	}

	return 0;
}

static int mainpageGetInputEvent(void)
{
	T_inputEvent inputEvent;
	static char touchFlag=0;
	unsigned char *buff =NULL;
	unsigned int  bpp = g_ptDisplayDevice->pixel/8;
	int x,y;

	getInputEvent(&inputEvent);
	if(inputEvent.type == INPUT_TYPE_TOUCH)
	{
		if(inputEvent.pressed ==1 && touchFlag==0)
		{
			if((inputEvent.x>setCity[0].xMin) && (inputEvent.x<setCity[0].xMax) && (inputEvent.y>setCity[0].yMin) && (inputEvent.y<setCity[0].yMax))
			{
				buff = g_ptDisplayDevice->getDisplayMmap();
				if(buff==NULL)
				{
					fprintf(stderr,"getDisplayMmap error\n");
					exit(1);
				}
				for(y= setCity[0].yMin; y< setCity[0].yMax; y++)
				{
					for(x= setCity[0].xMin ; x< setCity[0].xMax; x++)
					{
						buff[y*g_ptDisplayDevice->lineSize +x*bpp] = ~buff[y*g_ptDisplayDevice->lineSize +x*bpp];
						buff[y*g_ptDisplayDevice->lineSize +x*bpp+1] = ~buff[y*g_ptDisplayDevice->lineSize +x*bpp+1];
						buff[y*g_ptDisplayDevice->lineSize +x*bpp+2] = ~buff[y*g_ptDisplayDevice->lineSize +x*bpp+2];
					}
				}
				touchFlag =1;
			}
		}
		else if(inputEvent.pressed ==0 && touchFlag ==1)
		{
			if((inputEvent.x>setCity[0].xMin) && (inputEvent.x<setCity[0].xMax) && (inputEvent.y>setCity[0].yMin) && (inputEvent.y<setCity[0].yMax))
			{
				buff = g_ptDisplayDevice->getDisplayMmap();
				if(buff==NULL)
				{
					fprintf(stderr,"getDisplayMmap error\n");
					exit(1);
				}
				for(y= setCity[0].yMin; y< setCity[0].yMax; y++)
				{
					for(x= setCity[0].xMin ; x< setCity[0].xMax; x++)
					{
						buff[y*g_ptDisplayDevice->lineSize +x*bpp] = ~buff[y*g_ptDisplayDevice->lineSize +x*bpp];
						buff[y*g_ptDisplayDevice->lineSize +x*bpp+1] = ~buff[y*g_ptDisplayDevice->lineSize +x*bpp+1];
						buff[y*g_ptDisplayDevice->lineSize +x*bpp+2] = ~buff[y*g_ptDisplayDevice->lineSize +x*bpp+2];
					}
				}
				touchFlag =0;
				return 1;
			}
		}
	}

	return 0;
}

int mainpageInit(void)
{
	int ret;

	ret = registerPages(&mainpage);
	return ret;
}

